/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.book.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.entity.BkPublisher;
import com.thinkgem.jeesite.modules.book.service.BookPublisherService;
import com.thinkgem.jeesite.modules.sys.utils.PublisherUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出版社相关Controller
 *
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/book/publisher")
public class BookPublisherController extends BaseController{

    @Autowired
    private BookPublisherService bookPublisherService;

    @ModelAttribute
    public BkPublisher get(@RequestParam(required = false) String prId){
        if(StringUtils.isNotBlank(prId)){
            return bookPublisherService.get(prId);
        }
        else{
            return new BkPublisher();
        }
    }

    @RequiresPermissions("book:publisher:view")
    @RequestMapping(value = {"list", ""})
    public String list(BkPublisher bkPublisher, HttpServletRequest request, HttpServletResponse response, Model model){
        Page<BkPublisher> page = bookPublisherService.findPage(new Page<BkPublisher>(request, response), bkPublisher);
        model.addAttribute("page", page);
        return "modules/book/publisher/publisherList";
    }

    @RequiresPermissions("user")
    @RequestMapping("listData")
    public String listData(String publisher, Model model){
        //查询馆藏数量>0的书籍
        BkPublisher pub = new BkPublisher();
        pub.setPrName(publisher);
        List<BkPublisher> publisherList = bookPublisherService.findList(pub);
        model.addAttribute("publisherList", publisherList);
        model.addAttribute("publisher", publisher);
        return "modules/book/publisher/publisherListData";
    }

    @RequiresPermissions("book:publisher:edit")
    @RequestMapping("toEdit")
    public String toEdit(String prId, String message, Model model){

        if(StringUtils.isNotEmpty(message)){
            model.addAttribute("message", message);
        }
        BkPublisher bkPublisher;
        if(StringUtils.isNotEmpty(prId)){
            bkPublisher = bookPublisherService.get(prId);
            model.addAttribute("bkPublisher", bkPublisher);
        }

        return "modules/book/publisher/publisherEdit";
    }

    @RequiresPermissions("book:publisher:edit")
    @RequestMapping("save")
    public String save(BkPublisher bkPublisher, Model model){

        bkPublisher.setPrId(bkPublisher.getPrId());
        bookPublisherService.save(bkPublisher);

        model.addAttribute("message","保存成功！");
        // 刷新出版社缓存
        PublisherUtils.refreshPublisher();

        return "modules/book/publisher/success";
    }

    @RequiresPermissions("book:publisher:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id, Model model){
        BkPublisher publisher = new BkPublisher();
        publisher.setPrId(id);
        int result = bookPublisherService.delete(publisher);

        // 刷新出版社缓存
        PublisherUtils.refreshPublisher();

        if(result > 0){
            return new Result("保存成功！");
        }
        else{
            return new Result(Result.FAILURE, "保存失败！", null);
        }
    }

    @RequestMapping(value = "findAll")
    @ResponseBody
    public List<BkPublisher> findAll(){
        @SuppressWarnings("unchecked")
        List<BkPublisher> publisherList = (List<BkPublisher>)CacheUtils.get(PublisherUtils.CACHE_PUBLISHER_MAP);
        if(publisherList == null){
            publisherList = bookPublisherService.findAll();
            CacheUtils.put(PublisherUtils.CACHE_PUBLISHER_MAP, publisherList);
        }
        return publisherList;
    }

    @RequestMapping(value = "findAddress")
    @ResponseBody
    public BkPublisher findAddress(String prName){
        List<BkPublisher> publisherList = findAll();
        for(BkPublisher publisher:publisherList){
            if(publisher.getPrName().equals(prName)){
                return publisher;
            }
        }
        return null;
    }
}
